package com.zs.toolbox.web2j.system.core.controller.admin;


import com.zs.toolbox.web2j.common.exception.WtResponses;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.common.request.WtPageQuerySearchModel;
import com.zs.toolbox.web2j.common.request.WtPrimaryKeyReqeust;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtAdminBaseController;
import com.zs.toolbox.web2j.common.web.WtAuthContext;
import com.zs.toolbox.web2j.system.core.entity.WtRoleEntity;
import com.zs.toolbox.web2j.system.core.entity.WtRoleWithPermissionEntity;
import com.zs.toolbox.web2j.system.core.request.WtRolePermissionRequest;
import com.zs.toolbox.web2j.system.core.service.WtRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户基本信息操作
 * </p>
 *
 * @author eric.zhang
 * @since 2018-09-21
 */
@RequestMapping("/api/v1/admin/role/")
@Api(description = "角色管理")
@Slf4j
@RestController
public class WtAdminRoleController extends WtAdminBaseController<WtRoleService, WtRoleEntity,Long> {

    @Override
    public WtBaseResponse<WtPageBodyModel<WtRoleEntity>> getPage(@RequestBody WtPageQuerySearchModel wtPageQueryModel) {
        wtPageQueryModel.setSearchKey(getTableField(WtRoleEntity.class,wtPageQueryModel.getSearchKey()));
        return super.getPage(wtPageQueryModel);
    }

    @ApiOperation("根据ID查询详情，包含权限")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "getDetail",method = {RequestMethod.POST,RequestMethod.GET})
    public WtBaseResponse<WtRoleWithPermissionEntity> getRoleWithPermissionById(WtPrimaryKeyReqeust<Long> reqeust) {
        this.volidate(reqeust);
        WtRoleWithPermissionEntity wtRolePermissionEntity = baseService.getRoleWithPermissionById(reqeust.getId());
        return new WtBaseResponse<>(wtRolePermissionEntity);
    }

    @ApiOperation("删除原有权限，并使用新权限角色权限")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping(value = "saveRolePermission",method = {RequestMethod.POST,RequestMethod.GET})
    public WtBaseResponse<String> addOrDeleteRolePermission(WtRolePermissionRequest request) {
        this.volidate(request);
        baseService.saveRolePermission(request.getRoleId(),request.getPermissionIds(), WtAuthContext.getUserId());
        return WtResponses.SUCCESS;
    }
}
